package cn.crm.controller;

import cn.crm.pojo.CrmDepartment;
import cn.crm.service.DepartmentService;
import cn.crm.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    //分页展示部门信息
    @RequestMapping("/listDepartment")
    public String showDepartList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "2") int size, Model model){
         //调用service的分页功能
        PageUtil pageUtil = departmentService.findByPage(page, size);
        model.addAttribute("pageUtil",pageUtil);
        return "listDepartment";
    }

    //显示部门信息
    @RequestMapping("/addOrEditDepartment")
    public String showDepartInfo(String depId,Model model){
        //调用service根据id查询部门信息
        CrmDepartment departInfo = departmentService.findDepartInfo(depId);
        model.addAttribute("depart",departInfo);
        return "addOrEditDepartment";
    }

    //修改或添加部门信息
    @RequestMapping("/addOrEditDepart")
    public String updateDepart(CrmDepartment crmDepartment)
    {
          //调用service的的功能

        if(crmDepartment.getDepid().equals("")||crmDepartment.getDepid()==null){
            //实现部门的添加
            departmentService.addDepart(crmDepartment);
        }else {
            //实现部门的修改
            departmentService.updateDepartmentInfo(crmDepartment);
        }
         return "redirect:/department/listDepartment";//重新查询部门
    }

    //转到添加部门页面的controller
    @RequestMapping("/addDepartment")
    public String toAddPage(){

        return "addOrEditDepartment";
    }
}
